package two_xia.myPainter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CustomColorPanel extends JPanel {
    private DrawListener drawListener;
    private JPanel colorPanel;
    private JTextField redField, greenField, blueField;
    private StrokeChange strokeChange;

    public CustomColorPanel() {
    }

    public CustomColorPanel(DrawListener drawListener) {
        this.drawListener = drawListener;   //从主窗口那里获取到监听器并且定义
        this.strokeChange = new StrokeChange(drawListener);

        new StrokeChange(drawListener);
        setLayout(new BorderLayout());
        colorPanel = new JPanel();
        colorPanel.setBackground(Color.BLACK);
        add(colorPanel, BorderLayout.CENTER);

        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        redField = new JTextField("0", 3);
        greenField = new JTextField("0", 3);
        blueField = new JTextField("0", 3);

        JButton updateButton = new JButton("Update");
        updateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                updateColor();
            }
        });

        controlPanel.add(new JLabel("Red:"));
        controlPanel.add(redField);
        controlPanel.add(new JLabel("Green:"));
        controlPanel.add(greenField);
        controlPanel.add(new JLabel("Blue:"));
        controlPanel.add(blueField);
        controlPanel.add(updateButton);

        controlPanel.add(strokeChange, BorderLayout.EAST);//此为改变线条粗细的窗口类

        add(controlPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    public void setDrawListener(DrawListener drawListener) {
        this.drawListener = drawListener;
    }

    private void updateColor() {
        try {
            int red = Integer.parseInt(redField.getText());
            int green = Integer.parseInt(greenField.getText());
            int blue = Integer.parseInt(blueField.getText());

            if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255) {
                JOptionPane.showMessageDialog(this, "RGB values must be between 0 and 255.", "Invalid Input", JOptionPane.ERROR_MESSAGE);
                return;
            }
            Color color = new Color(red, green, blue);
            colorPanel.setBackground(color);
            if (drawListener != null) {
                drawListener.onColorChanged(color);
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Please enter valid numeric values for RGB.", "Invalid Input", JOptionPane.ERROR_MESSAGE);
        }
    }

    //颜色同步
    public void Synchronous(Color color) {
        colorPanel.setBackground(color);
        // 分别获取Color的R,G,B值
        int R = color.getRed();
        int G = color.getGreen();
        int B = color.getBlue();
        // 分别将获取到的R,G,B及HEX值显示在界面中
        redField.setText(String.valueOf(R));
        greenField.setText(String.valueOf(G));
        blueField.setText(String.valueOf(B));
    }

}
